/*
 * Attribute.java
 *
 * Created on 30 de junio de 2006, 13:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ocp.context;

/**
 *
 * @author Ignacio Nieto Carvajal
 */
public class Attribute implements java.io.Serializable {
    
    protected AttributeDefinition ad;
    
    protected Object value;
    
    /** Creates a new instance of Attribute */
    public Attribute(AttributeDefinition ad, Object value) throws ContextException {
        if (!ad.getType().isInstance(value))
            throw new ContextException ("Attribute value must match attribute definition in attribute "+ad.getName()+
                "!\nfound: "+value.getClass().toString()+", expected: "+ad.getType().toString());
        this.ad = ad;
        this.value = value;
    }
    
    public AttributeDefinition getAttributeDefinition() { return ad; }
    public Object getValue()    { return value; }
    
    public String toXML() {
        String xml = "<Attribute name=\""+ad.getName()+"\" type=\""+ad.getType()+"\">";
        xml += "<Value>"+value.toString()+"</Value>";
        xml += "</Attribute>\n";
        return xml;
    }
}